package com.itheima.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class SendSms {
    private static final String ACCESSKEY_ID = "LTAI4FkhWn3FYsEWrYpcxC5E";  //写自己的AccessID
    private static final String ACCESS_KEY_SECRET = "UqCmYo5ZxdLSkBcNRiJprPwhkNzCzZ"; //写自己的AccessKeySecret
    private static final String SING_NAME ="传智健康"; //改成自己的签名
    public static final String VALIDATE_CODE_TEMPLATE = "SMS_181867429";  //验证码
    public static final String VALIDATE_CODE = "SMS_181858182";        //短信通知


    /**
     * 发送短信的工具类
     * @param PhoneNumbers 接收短信的手机号
     * @param templateCode 发送短信的模板
     * @param code 发送的验证码
     * @return
     */
    public static CommonResponse sendSms(String PhoneNumbers,String templateCode, String code ) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESSKEY_ID, ACCESS_KEY_SECRET );
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", PhoneNumbers);
        request.putQueryParameter("SignName", SING_NAME);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":" + code + "}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {
        CommonResponse response = sendSms("17721673052", SendSms.VALIDATE_CODE_TEMPLATE, "8888");
        System.out.println(response);
    }
}
